﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Threading;

namespace BSU.CSCE.Utilities
{
    public static class EnumTranslator
    {
        private static readonly ResourceManager _resourceManager;

        static EnumTranslator()
        {
            Assembly globalResources = Assembly.Load("App_GlobalResources");
            _resourceManager = new ResourceManager("Resources.Enums", globalResources);
        }

        public static string Translate(Enum value)
        {
            string resourceName = GetResourceName(value);
            return _resourceManager.GetString(resourceName, Thread.CurrentThread.CurrentCulture);
        }

        public static IList<EnumItem> Translate(Type enumType)
        {
            Array values = Enum.GetValues(enumType);
            IList<EnumItem> items = new List<EnumItem>();

            for (int i = 0; i < values.Length; i++)
            {
                int value = Convert.ToInt32(values.GetValue(i));
                string text = Translate((Enum)values.GetValue(i));
                EnumItem item = new EnumItem(value, text);
                items.Add(item);
            }

            return items;
        }

        private static string GetResourceName(Enum value)
        {
            Type type = value.GetType();
            return type.Name + "_" + value;
        }
    }
}
